'use strict'

import axios from 'axios'
import i18n from './i18n.js'
import configBaseUrl from './setBaseUrl'

let config = {
  baseURL: configBaseUrl,
  method: 'get',
  // 请求头信息
  headers: {
    'Content-Type': 'application/json;charset=UTF-8'
  },
  // 参数
  data: {},
  // 设置超时时间
  timeout: 200000,
  // Check cross-site Access-Control
  withCredentials: false,
  // 返回数据类型
  responseType: 'json'
}

const _axios = axios.create(config)

_axios.interceptors.request.use(
  function (config) {
    // 带上 token , 可以结合 vuex 或者重 localStorage
    config.headers['X-CAD-Platform'] = 'Web'
    // if (store.getters.currentUser) {
    //   config.headers['X-CAD-Current-Agency-Id'] = store.state.user.currentAgencyId
    //   config.headers['X-CAD-Language'] = store.state.user.language
    //   config.headers['X-CAD-UID'] = store.state.user.uid
    //   config.headers['X-CAD-Token'] = store.state.user.token
    //   config.headers['X-CAD-TimeZoneOffset'] = -(new Date().getTimezoneOffset() / 60)
    // }
    return config
  },
  function (error) {
    // 请求错误时做些事(接口错误、超时等)
    return Promise.reject(error)
  }
)

// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    let data
    // IE9时response.data是undefined，因此需要使用response.request.responseText(Stringify后的字符串)
    if (response.data === undefined || response.data === '') {
      data = JSON.parse(response.request.responseText)
    } else {
      data = response.data
    }
    return data
  },
  function (err) {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = i18n.global.t('loc.badRequest') // '请求错误'
          break

        case 401:
          err.message = i18n.global.t('loc.logonFailure') // '用户错误'
          // 跳转到当前路径的登录界面
          // toSignOut()
          break

        case 403:
          err.message = i18n.global.t('loc.accessDenied') // '拒绝访问'
          break

        case 404:
          err.message = i18n.global.t('loc.requestAddressError') + ':' + err.response.config.url // `请求地址出错: ${err.response.config.url}`
          break

        case 408:
          err.message = i18n.global.t('loc.requestTimeout') // '请求超时'
          break

        case 500:
          err.message = i18n.global.t('loc.serverInternalError') // '服务器内部错误'
          break

        case 501:
          err.message = i18n.global.t('loc.serviceNotImplemented') // '服务未实现'
          break

        case 502:
          err.message = i18n.global.t('loc.badGateway') // '网关错误'
          break

        case 503:
          err.message = i18n.global.t('loc.serviceUnavailable') // '服务不可用'
          break

        case 504:
          err.message = i18n.global.t('loc.gatewayTimeout') // '网关超时'
          break

        case 505:
          err.message = i18n.global.t('loc.httpNotSupport') // 'HTTP版本不受支持'
          break

        default:
      }
    }
    if (err.response) {
      if (err.response.data) {
        err.message = err.response.data.message
      }
    }
    return Promise.reject(err)
  }
)
window.$axios = _axios
export default _axios
